package cn.yd.rb.controller;

import cn.yd.rb.po.*;
import cn.yd.rb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@Controller
@RequestMapping("/comAndScore")
public class comAndScoreController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private NovelService novelService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private EnsureService ensureService;
    @RequestMapping("/findcomment")
    public String findcomment(Model model,HttpSession session){
        Novel  novelSession = (Novel)session.getAttribute("novelSession");
        User  user = (User)session.getAttribute("user");
        //将评论传到评论页面
        List<Comment> commentList =commentService.findCommentAll(novelSession.getId());
        model.addAttribute("commentList",commentList);
        //将打分传到评论页面
        if (user!=null){
            Grade grade = gradeService.findGrade(novelSession.getId(),user.getId());
            model.addAttribute("grade",grade);
        }
        //将点赞传到评论页面
        if (user!=null){
            List<Ensure> ensureList = ensureService.findEnsure(novelSession.getId(),user.getId());
            model.addAttribute("ensureList",ensureList);
        }
        //将小说传到评论页面
        Novel novel= novelService.findNovelById(novelSession.getId());
        model.addAttribute("novel",novel);
        return "comment";
    }
    @RequestMapping("/insertComment")
    public String insertComment(Model model,HttpSession session,Comment comment){
        User  user = (User)session.getAttribute("user");
        if (user!=null){
            comment.setCreatetime(new Date());
            comment.setZcount(0);
            comment.setUserId(user.getId());
            comment.setUsername(user.getUsername());
            commentService.insertComment(comment);
        }
       /* List<Comment> commentList =commentService.findCommentAll(comment.getNovelId());
        model.addAttribute("commentList",commentList);*/
        return "redirect:/comAndScore/findcomment";
    }
    @RequestMapping("/insertGrade")
    public String insertGrade(Model model,HttpSession session,Grade grade){

        User  user = (User)session.getAttribute("user");
        if(user !=null){
            Novel  novelSession = (Novel)session.getAttribute("novelSession");
            grade.setNovelid(novelSession.getId());
            grade.setUserid(user.getId());
            gradeService.insertGrade(grade);
        }

        return "redirect:/comAndScore/findcomment";
    }

    @RequestMapping("/insertEnsure")
    public String insertEnsure(Model model,HttpSession session,Integer islike,Integer commentId){

        User  user = (User)session.getAttribute("user");
        if(user !=null){
            Novel  novelSession = (Novel)session.getAttribute("novelSession");
            Ensure ensure =new Ensure();
            ensure.setIsLike(islike);
            ensure.setUserId(user.getId());
            ensure.setCommentId(commentId);
            ensure.setNovelId(novelSession.getId());
            ensureService.insertEnsure(ensure);
        }

        return "redirect:/comAndScore/findcomment";
    }
}
